﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmTBTrangThietBi : Form
    {
        private IList<TRANGBITRANGTB> lstTBThietBi;
        private IList<LOAIPHONG> lstLoaiPhong;
        private THIETBI_DV Thietbi;
        public static FrmTBTrangThietBi frm = null;

        public FrmTBTrangThietBi()
        {
            InitializeComponent();
            InitTBThietBi();
            InitLoaiPhong();
            frm = this;
        }

        private void InitTBThietBi()
        {
            TRANGBITRANGTB[] outTRANGBITRANGTB;
            var client = new ServiceQLKSClient();
            var error = client.doparserTBThietBi(out outTRANGBITRANGTB, OPTCodeQLKS.OPTCode_TBTHIETBI_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_TBTHIETBI_GETALL_SUCCESS)
            {
                lstTBThietBi = outTRANGBITRANGTB.ToList();
                tRANGBITRANGTBBindingSource.DataSource = lstTBThietBi;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InitLoaiPhong()
        {
            LOAIPHONG[] outLOAIPHONG;
            var client = new ServiceQLKSClient();
            var error = client.doparserLoaiPhong(out outLOAIPHONG, OPTCodeQLKS.OPTCode_LOAIPHONG_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_LOAIPHONG_GETALL_SUCCESS)
            {
                lstLoaiPhong = outLOAIPHONG.ToList();
                lOAIPHONGBindingSource.DataSource = lstLoaiPhong;
                lOAIPHONGBindingSource1.DataSource = lstLoaiPhong;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InsertOrUpdate()
        {
            TRANGBITRANGTB TBthietbi = new TRANGBITRANGTB();
            if (txttentrangbi1 == null || txtsoluong == null)
            {
                MessageBox.Show("Lỗi! Đề nghị kiểm tra lại dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                TBthietbi.SOLUONG = Int32.Parse(txtsoluong.Text);
                TBthietbi.MSTB = Thietbi.MSTB;
                TBthietbi.MSLOAIPHONG = cboLoaiPhong.SelectedValue.ToString();
            }
            TRANGBITRANGTB[] outTRANGBITRANGTB;
            var client = new ServiceQLKSClient();
            var error = client.doparserTBThietBi(out outTRANGBITRANGTB, OPTCodeQLKS.OPTCode_TBTHIETBI_INSERTUPDATE, TBthietbi);
            if (error == ErrorCodeQLKS.CODE_TBTHIETBI_INSERTUPDATE_SUCCESS)
            {
                InitTBThietBi();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void DeleteTBThietBi()
        {
            var item = dataGridView1.CurrentRow.DataBoundItem as TRANGBITRANGTB;
            if (item != null)
            {
                TRANGBITRANGTB[] outTRANGBITRANGTB;
                var client = new ServiceQLKSClient();
                var error = client.doparserTBThietBi(out outTRANGBITRANGTB, OPTCodeQLKS.OPTCode_TBTHIETBI_DELETE, item);
                if (error == ErrorCodeQLKS.CODE_TBTHIETBI_DELETE_SUCCESS)
                {
                    InitTBThietBi();
                }
                try
                {
                    if (client.State != System.ServiceModel.CommunicationState.Faulted)
                    {
                        client.Close();
                    }
                }
                catch (Exception ex)
                {
                    client.Abort();
                }
            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = dataGridView1.CurrentRow.DataBoundItem as TRANGBITRANGTB;
            if (item != null)
            {
                foreach (LOAIPHONG lp in cboLoaiPhong.Items)
                {
                    if (item.MSLOAIPHONG.Equals(lp.MSLOAIPHONG))
                    {
                        cboLoaiPhong.SelectedItem = lp;
                        break;
                    }
                }
                txttentrangbi1.Text = item.TENTHIETBI;
                txtsoluong.Text = item.SOLUONG.ToString();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                FrmTimKiemThietBi frm = new FrmTimKiemThietBi(txttentrangbi1.Text);
                frm.ShowDialog();
                Thietbi = frm.ThietBi;
                txttentrangbi1.Text = Thietbi.TENTHIETBI;
                txtsoluong.Focus();
            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            txttentrangbi1.Clear();
            txttentrangbi1.Focus();
            txtsoluong.Clear();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            InsertOrUpdate();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DeleteTBThietBi();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTBTrangThietBi_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }

        private void cboLocLoaiP_SelectedIndexChanged(object sender, EventArgs e)
        {
            IList<TRANGBITRANGTB> lsttmp;
            try
            {
                lsttmp = lstTBThietBi.Where(x => x.MSLOAIPHONG.Equals(cboLocLoaiP.SelectedValue.ToString())).ToList();
                tRANGBITRANGTBBindingSource.DataSource = lsttmp;
            }
            catch (Exception ex)
            {
            }
            
        }
    }
}
